package com.linln.component.shiro;

import com.linln.common.utils.NetworkUtil;
import com.linln.modules.system.domain.User;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.slf4j.MDC;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理session超时问题拦截器
 * @author 小懒虫
 * @date 2018/8/14
 */
public class UserAuthFilter extends AccessControlFilter {
    private static final Logger logger = LoggerFactory.getLogger(UserAuthFilter.class);

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object mappedValue) {
        if (isLoginRequest(servletRequest, servletResponse)) {
            return true;
        } else {
            Subject subject = getSubject(servletRequest, servletResponse);
            HttpServletRequest request = WebUtils.toHttp(servletRequest);
            HttpServletResponse response = WebUtils.toHttp(servletResponse);

            // 获得客户的网络地址
            String address = NetworkUtil.getIpAddress(request);
            // 把网络地址放入MDC中. 那么在在layout pattern 中通过使用 %x，就可在每条日之中增加网络地址的信息.
            MDC.put("address", address);
            User user = ShiroUtil.getSubject();
            if(null != user){
                MDC.put("username", user.getUsername());
            }

            return subject.getPrincipal() != null;
        }
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpRequest = WebUtils.toHttp(request);
        HttpServletResponse httpResponse = WebUtils.toHttp(response);

        if (httpRequest.getHeader("X-Requested-With") != null
                && "XMLHttpRequest".equalsIgnoreCase(httpRequest.getHeader("X-Requested-With"))) {
            httpResponse.sendError(HttpStatus.UNAUTHORIZED.value());
        } else {
            redirectToLogin(request, response);
        }
        return false;
    }

//    @Override
//    public void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
//        HttpServletRequest request = WebUtils.toHttp(servletRequest);
//        HttpServletResponse response = WebUtils.toHttp(servletResponse);
//
//        // 获得客户的网络地址
//        String address = NetworkUtil.getIpAddress(request);
//        // 把网络地址放入MDC中. 那么在在layout pattern 中通过使用 %x，就可在每条日之中增加网络地址的信息.
//        MDC.put("address", address);
////        ThreadContext.put("address", address);
//
//
//        // 获取当前登录的用户
//        User user = ShiroUtil.getSubject();
//        if(null == user){
//            filterChain.doFilter(request, response);
////            ThreadContext.clearAll();
//            MDC.clear();
//            return ;
//        }
//
//
//        MDC.put("username", user.getUsername());
////        ThreadContext.put("username", user.getUsername());
//        //继续处理其他的filter链.
//
//        filterChain.doFilter(servletRequest, servletResponse);
////        ThreadContext.clearAll();
//
//        MDC.remove("username");
//        MDC.remove("address");
//
//
//    }
}
